/*******************************************************************************
 * Dreampalace
 *******************************************************************************
 * 
 * Dreampalace is a special dreamdiary which helps dreamers to understand their
 * dreams. You can find also many features in Dreampalace to learn and hold the
 * lucid state in your dreams.
 * 
 * *****************************************************************************
 *
 * Program	   : Dreampalace
 * Autor           : PRo (Peter Rogge)
 * Email           : rogge-peter@gmx.de
 * Homepage        : http://code.google.com/p/dreampalace
 * License	   : GNU General Public License 3.0
 * 
 *******************************************************************************
 * 
 * This file is part of the project Dreampalace. Dreampalace is free software:
 * you can redistribute it and/or modify it under the terms of the GNU General 
 * Public License as published by the Free Software Foundation in version 3 of
 * the License only.
 * 
 * Dreampalace is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * Dreampalace. If not, see <http://www.gnu.org/licenses/>.
 * 
 * The Dreampalace project designates this particular file as subject to the
 * "Classpath" exception as provided by the Dreampalace project in the GPL 
 * Version 3 section of the License file that accompanied this code.
 * 
 *******************************************************************************
 */
package de.pro.dreampalace.file.manager;

import com.zenjava.jfxflow.actvity.Activity;
import com.zenjava.jfxflow.actvity.FxmlLoader;
import com.zenjava.jfxflow.navigation.NavigationManager;
import de.pro.dreampalace.database.api.IDatabaseManager;
import de.pro.dreampalace.file.about.AboutActivity;
import de.pro.dreampalace.file.api.EFileTyp;
import de.pro.dreampalace.file.api.IFile;
import de.pro.dreampalace.file.links.api.ILinksManager;
import de.pro.dreampalace.file.manager.api.IFileManager;
import de.pro.dreampalace.file.notes.NotesEditorActivity;
import de.pro.dreampalace.spi.File;
import java.sql.SQLException;
import java.util.logging.Logger;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;

/**
 *
 * @author Peter Rogge (PRo) <rogge-peter@gmx.de>
 */
@ServiceProvider(service=IFileManager.class)
public final class FileManager implements IFileManager {
    
    private NavigationManager navigationManager = null;
    
    public FileManager() {
        this.init();
    }
    
    private void init() {
        final ILinksManager lm = Lookup.getDefault().lookup(ILinksManager.class);
        final IDatabaseManager dm = Lookup.getDefault().lookup(IDatabaseManager.class);
        lm.setConnection(dm.getConnection());
        lm.checkTables();
    }

    @Override
    public void checkTables() {
        this.checkTableFile();
    }
    
    private void checkTableFile() {
        // Check if the table exists...
        final IDatabaseManager dm = Lookup.getDefault().lookup(
                IDatabaseManager.class);
        final boolean isCreated = dm.checkTable("File"); // NOI18N
        if (isCreated) {
            Logger.getLogger(FileManager.class.getName()).info(
                "Table FILE is created."); // NOI18N
            return;
        }
        
        // Otherwise create the table
        this.createTableFile();
    }
    
    private void createTableFile() {
        Logger.getLogger(FileManager.class.getName()).info(
            "Create table FILE ..."); // NOI18N
        
        final StringBuilder sql = new StringBuilder();
        sql.append("CREATE TABLE File ("); // NOI18N
            sql.append("ID BIGINT NOT NULL, "); // NOI18N
            sql.append("Title VARCHAR(64) NOT NULL, "); // NOI18N
            sql.append("Date DATE NOT NULL, "); // NOI18N
            sql.append("Content CLOB NOT NULL, "); // NOI18N
            sql.append("FileTyp VARCHAR(32) NOT NULL, "); // NOI18N
        sql.append("PRIMARY KEY(ID))"); // NOI18N
        
        final IDatabaseManager dm = Lookup.getDefault().lookup(IDatabaseManager.class);
        try {
            dm.createTable(sql.toString());
            
            Logger.getLogger(FileManager.class.getName()).info(
                "Table FILE is created."); // NOI18N
        } catch (SQLException ex) {
            Logger.getLogger(FileManager.class.getName()).warning(
                "Can't create the table FILE:"); // NOI18N
            Logger.getLogger(FileManager.class.getName()).warning(
                ex.getMessage());
        }
    }
    
    @Override
    public final IFile getDefaultFile(final EFileTyp fileTyp) {
        // TODO get the defaultfile from the new module DreampalaceFileXY
        final IFile file = new File(fileTyp);
//        switch(fileTyp) {
//            case NOTES_EDITOR: {
//                file.setTitle("Notes"); // NOI18N
//                break;
//            }
//        }
        
        return file;
    }

    @Override
    public final Activity getPlace(final EFileTyp fileTyp, final File file) {
        final FxmlLoader loader = new FxmlLoader();
//        final IDatabaseManager dm = Lookup.getDefault().lookup(IDatabaseManager.class);

        switch(fileTyp) {
            case ABOUT: {
                final AboutActivity activity = loader.load(fileTyp.getPlace(),
                    NbBundle.getBundle(AboutActivity.class));
                return activity;
            }
            case LINKS: {
                final ILinksManager lm = Lookup.getDefault().lookup(ILinksManager.class);
                return lm.getActivity(fileTyp);
            }
        }
        
        return null;
    }

    @Override
    public final Activity getPlacePreview(final EFileTyp fileTyp, final File file) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public final Activity getPlaceEditor(final EFileTyp fileTyp, final File file) {
        final FxmlLoader loader = new FxmlLoader();

        switch(fileTyp) {
            case LINKS_EDITOR: {
                final ILinksManager lm = Lookup.getDefault().lookup(ILinksManager.class);
                return lm.getActivity(fileTyp);
            }
            case NOTES_EDITOR: {
                final NotesEditorActivity activity = loader.load(fileTyp.getPlace(),
                    NbBundle.getBundle(NotesEditorActivity.class));
                final IDatabaseManager dm = Lookup.getDefault().lookup(
                        IDatabaseManager.class);
                activity.setConnection(dm.getConnection());
                activity.setNavigationManager(navigationManager);
                
                return activity;
            }
        }
        
        return null;
    }

    @Override
    public void setNavigationManager(NavigationManager navigationManager) {
        this.navigationManager = navigationManager;
        
        final ILinksManager lm = Lookup.getDefault().lookup(ILinksManager.class);
        lm.setNavigationManager(navigationManager);
    }
}
